package com.sky.common.generate.core;

public class GenerateApi extends GenerateBase {

    public GenerateApi(GenerateBase generateBase) {
        super(generateBase.packagePath, generateBase.tableName, generateBase.moduleName, generateBase.className,
                generateBase.fieldList, generateBase.fieldMethod);
    }

    public void execute() {
        StringBuilder builder = new StringBuilder();
        String generatePackage = generatePackage(packagePath + "." + moduleName + ".controller");
        String generateClass = generateClass(className);
        String generateAttribute = generateAttribute(className);

        builder.append(generatePackage);
        builder.append(generateApiImport(generateImport(generateClass + generateAttribute), packagePath, moduleName,
                className));
        builder.append(generateClass);
        builder.append(generateAttribute);

        writeGenerateFile(builder.append("}").toString().getBytes(), getGeneratePath("controller", "Api"));
    }

    /**
     * 生成引用路径
     **/
    public String generateApiImport(String importJavaPath, String packagePath, String moduleName, String className) {
        return importJavaPath + "import " + packagePath + "." + moduleName + "." + "entity" + "." + className + ";\n"
                + "import " + packagePath + "." + moduleName + "." + "entity" + "." + className + "Param;\n"
                + "import " + packagePath + "." + moduleName + "." + "entity" + "." + className + "VO;\n" + "\n\n";
    }

    /**
     * 生成类
     **/
    public String generateClass(String className) {
        return "//(替换成自己服务的注解)@XXServer(path = \"/" + className.toLowerCase() + "\", contextId = \"" + className
                + "Api\")\npublic interface " + className + "Api" + " {\n\n";
    }

    /**
     * 生成接口方法
     **/
    public String generateAttribute(String className) {
        StringBuilder b = new StringBuilder();
        String javaDataType = fieldInfo != null ? fieldInfo.getJavaDataType() : "Serializable";
        b.append("	/** 查询列表(分页) **/\n");
        b.append("	@RequestMapping(\"/get").append(className).append("Page\")\n");
        b.append("	PageInfo<").append(className).append("VO> get").append(className).append("Page(@RequestBody ")
                .append(className).append("Param ").append("param);\n\n");

        b.append("	/** 查询列表 **/\n");
        b.append("	@RequestMapping(\"/get").append(className).append("List\")\n");
        b.append("	List<").append(className).append("VO> get").append(className).append("List(@RequestBody ").append(className).append("Param param);\n\n");

        b.append("    /** 根据ID获取详情 **/\n");
        b.append("	@RequestMapping(\"/get").append(className).append("ById\")\n");
        b.append("	").append(className).append("VO get").append(className).append("ById(@RequestParam(\"id\") ").append(javaDataType).append(" id);\n\n");

        b.append("    /** 新增 **/\n");
        b.append("    @RequestMapping(\"/add").append(className).append("\")\n");
        b.append("    Integer add").append(className).append("(@RequestBody ").append(className).append(" ").append(lower(className)).append(");\n\n");

        b.append("    /** 批量新增 **/\n");
        b.append("    @RequestMapping(\"/addBatch").append(className).append("\")\n");
        b.append("    Integer addBatch").append(className).append("(@RequestBody List<").append(className).append("> " + lower(className) + "List);\n\n");

        b.append("    /** 编辑 **/\n");
        b.append("    @RequestMapping(\"/edit").append(className).append("\")\n");
        b.append("    Integer edit").append(className).append("(@RequestBody ").append(className).append(" " + lower(className) + ");\n\n");

        b.append("    /** 批量更新 **/\n");
        b.append("    @RequestMapping(\"/editBatch").append(className).append("\")\n");
        b.append("    Integer editBatch").append(className).append("(@RequestBody List<").append(className).append("> " + lower(className) + "List);\n\n");

        b.append("    /** 根据ID删除 **/\n");
        b.append("	@RequestMapping(\"/del").append(className).append("ById\")\n");
        b.append("    Integer del").append(className).append("ById(@RequestParam(\"id\") ").append(javaDataType).append(" id);\n\n");

        b.append("    /** 根据ID批量删除 **/\n");
        b.append("	@RequestMapping(\"/del").append(className).append("ByIds\")\n");
        b.append("    Integer del").append(className).append("ByIds(@RequestBody List<").append(javaDataType).append("> ids);\n\n");
        return b.toString();
    }

}
